package org.ckq.base;

import java.util.Iterator;

/**
 * Created by chenkaiqing on 17/8/28.
 */
public class LinkedList<E> implements Iterable<E>{
    private Element head;

    public int put(E e){
        E el = find(e);
        //override if exist
        if(el != null){
            el = e;
            return 0;
        }else {
            //put on head if not exist
            Element newHead = new Element();
            newHead.e = e;
            newHead.next = head;
            head = newHead;
            return 1;
        }
    }

    public E get(E e){
        E el = find(e);
        return el == null ? null : el;
    }

    public void remove(E e){
        Element cur = head;
        if(cur == null || e == null){
            return;
        }
        Element pre = null;
        while(cur.next != null){
            if(e.equals(cur.e)){
                //first
                if(pre == null){
                    head = cur.next;
                    cur.next = null;
                    return;
                }else {//middle
                    pre.next = cur.next;
                    cur.next = null;
                    return;
                }
            }
            pre = cur;
            cur = cur.next;
        }
        //last
        if(e.equals(cur.e)){
            pre.next = null;
        }

//        Element element = find(key);
//        if(element != null){
//            Element pre = element.previous;
//            Element next = element.next;
//            if(pre == null && next == null){
//                head = null;
//                return;
//            }
//            if(pre == null && next != null){
//                next.previous = null;
//                head = next;
//                element.next = null;
//                return;
//            }
//            if(pre != null && next == null){
//                pre.next = null;
//                element.previous = null;
//                return;
//            }
//            if(pre != null && next != null){
//                pre.next = next;
//                next.previous = pre;
//                element.previous = null;
//                element.next = null;
//                return;
//            }
//        }

    }

    private E find(E e){
        if(e == null){
            return null;
        }
        Iterator<E> itr = iterator();
        while(itr.hasNext()){
            E el = itr.next();
            if(e.equals(el)){
                return el;
            }
        }
        return null;
    }

    @Override
    public Iterator iterator() {
        class Itr implements Iterator{
            Element nextEl = head;

            @Override
            public boolean hasNext() {
                return nextEl == null ? false : true;
            }

            @Override
            public E next() {
                Element e = nextEl;
                nextEl = nextEl.next;
                return e.e;
            }
        }
        return new Itr();
    }

     private class Element{
        private E e;
        private Element next;
        public Element(){
            super();
        }
        public Element(E e,Element next){
            this.e = e;
            this.next = next;
        }
    }
}
